package com.hashcoding.eyes.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hashcoding.eyes.EyesApplication;
import com.hashcoding.eyes.R;
import com.hashcoding.eyes.module.utils.NetworkUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 空页面
 *
 * @author markmjw
 * @date 2014-10-29
 */
public class EmptyLayout extends FrameLayout implements View.OnClickListener {
    public static final int STATE_NETWORK_ERROR = 1;
    public static final int STATE_LOADING = 2;
    public static final int STATE_EMPTY = 3;
    public static final int STATE_HIDE = 4;
    public static final int STATE_EMPTY_ENABLE_CLICK = 5;

    @Bind(R.id.empty_layout)
    LinearLayout mLayout;

    @Bind(R.id.empty_progress)
    ProgressBar mProgressBar;

    @Bind(R.id.empty_text)
    TextView mTextView;

    @Bind(R.id.empty_image)
    ImageView mImageView;

    private boolean mClickEnable = true;
    private int mErrorState;
    private String mEmptyHint = "";

    private OnClickListener mOnClickListener;

    public EmptyLayout(Context context) {
        super(context);
        init(context);
    }

    public EmptyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View view = View.inflate(context, R.layout.vw_empty_layout, this);
        ButterKnife.bind(this, view);

        setBackgroundColor(getResources().getColor(R.color.activity_background));
        setOnClickListener(this);
    }

    public void dismiss() {
        mErrorState = STATE_HIDE;
        setVisibility(View.GONE);
    }

    public int getErrorState() {
        return mErrorState;
    }

    public boolean isLoadError() {
        return mErrorState == STATE_NETWORK_ERROR;
    }

    public boolean isLoading() {
        return mErrorState == STATE_LOADING;
    }

    public void setErrorMessage(String msg) {
        mTextView.setText(msg);
    }

    public void setState(int state) {
        setVisibility(View.VISIBLE);

        switch (state) {
            case STATE_NETWORK_ERROR:
                mErrorState = STATE_NETWORK_ERROR;
                if (NetworkUtil.isConnected(EyesApplication.gContext)) {
                    mTextView.setText(R.string.error_view_load_error_click_to_refresh);
                    mImageView.setBackgroundResource(R.drawable.icon_error);
                } else {
                    mTextView.setText(R.string.error_view_network_error_click_to_refresh);
                    mImageView.setBackgroundResource(R.drawable.icon_error);
                }
                mImageView.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.GONE);
                mClickEnable = true;
                break;

            case STATE_LOADING:
                mErrorState = STATE_LOADING;
                mProgressBar.setVisibility(View.VISIBLE);
                mImageView.setVisibility(View.GONE);
                mTextView.setText(R.string.error_view_loading);
                mClickEnable = false;
                break;

            case STATE_EMPTY:
                mErrorState = STATE_EMPTY;
                mImageView.setBackgroundResource(R.drawable.icon_empty);
                mImageView.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.GONE);
                setEmptyText();
                mClickEnable = false;
                break;

            case STATE_HIDE:
                setVisibility(View.GONE);
                break;

            case STATE_EMPTY_ENABLE_CLICK:
                mErrorState = STATE_EMPTY_ENABLE_CLICK;
                mImageView.setBackgroundResource(R.drawable.icon_empty);
                mImageView.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.GONE);
                setEmptyText();
                mClickEnable = true;
                break;

            default:
                break;
        }
    }

    public void setEmptyTextHint(String textHint) {
        mEmptyHint = textHint;
    }

    public void setEmptyImage(int emptyId) {
        mImageView.setBackgroundResource(emptyId);
    }

    public void setEmptyImageVisibility(int visibility) {
        mImageView.setVisibility(visibility);
    }

    public void setEmptyText(String text) {
        mTextView.setText(text);
    }

    public void setOnLayoutClickListener(OnClickListener listener) {
        mOnClickListener = listener;
    }

    public void setEmptyText() {
        if (!TextUtils.isEmpty(mEmptyHint)) {
            mTextView.setText(mEmptyHint);
        } else {
            mTextView.setText(R.string.error_view_no_data);
        }
    }

    @Override
    public void onClick(View v) {
        if (mClickEnable && null != mOnClickListener) {
            mOnClickListener.onClick(v);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == View.GONE)
            mErrorState = STATE_HIDE;
        super.setVisibility(visibility);
    }
}
